//
//  LocationManager.m
//  U_Airplane
//
//  Created by iOS on 2017/11/30.
//  Copyright © 2017年 cj. All rights reserved.
//

#import "LocationManager.h"
#import <CoreLocation/CoreLocation.h>

@interface LocationManager()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) NSString *currentCity; //当前城市
@end

@implementation LocationManager

+(instancetype)shareInstance{
    static LocationManager *locationManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        locationManager = [[LocationManager alloc] init];
    });
    return locationManager;
}

- (void)startLocate {
    //判断定位功能是否打开
    if ([CLLocationManager locationServicesEnabled]) {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        [self.locationManager requestWhenInUseAuthorization];
        _currentCity = [[NSString alloc] init];
        [self.locationManager startUpdatingLocation];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    }
}

#pragma mark CoreLocation delegate
#pragma mark -- 定位成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    [self.locationManager stopUpdatingLocation];
    CLLocation *currentLocation = [locations lastObject];
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    //   CLLocationCoordinate2D oldCoordinate = currentLocation.coordinate;
    
    NSDictionary *dict =@{@"latitude":@(currentLocation.coordinate.latitude),@"longitude":@(currentLocation.coordinate.longitude)};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:Notice_RefreshMapCenter object:nil userInfo:dict];
    DefineWeakSelf;
    //反编码
    [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count > 0) {
            CLPlacemark *placeMark = placemarks[0];
            _currentCity = placeMark.locality;
            if (!_currentCity) {
                _currentCity = @"无法定位当前城市";
            }else {
                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                if ([_currentCity rangeOfString:@"市"].location != NSNotFound) {
                    _currentCity = [_currentCity stringByReplacingOccurrencesOfString:@"市" withString:@""];
                }
                
                if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(refreshCurrentLocation:)]) {
                    [weakSelf.delegate refreshCurrentLocation:_currentCity];
                }
                if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(getUserCurrentLat:)]) {
                    [weakSelf.delegate getUserCurrentLat:dict];
                }
            }
        }
        else if (error == nil && placemarks.count == 0) {
            NSLog(@"No location and error return");
        }
        else if (error) {
            NSLog(@"location error: %@ ",error);
        }
    }];
}

//定位失败弹出提示框,点击"打开定位"按钮,会打开系统的设置,提示打开定位服务
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    [ToolHelper showAlert:@"允许\"定位\"提示" withMsg:@"请在设置中打开定位" cancelBtnTitle:@"取消" okBtnTitle:@"打开定位" withVc:_curVc confirmBlock:^{
        //打开定位设置
        NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:settingsURL];
    }];
}

@end
